package 链表;

/* 单向链表 */
public class SingleLinkList<E> extends AbstractList<E> {
//    private int size;

    // first代表最开始的node节点
    private Node<E> first;

    // java里类里面的私有类写成静态的
    private static class Node<E>{
        E element;
        Node<E> next;

        public Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }
    }



    // 先获取节点，再获取节点里面的element
    @Override
    public E get(int index) {
        return get_node(index).element;
    }

    @Override
    public int indexOf(E element) {
        if(element == null){
            Node<E> node = first;
            for (int i = 0;i<size;i++){
                if (node.element == null){
                    return i;
                }
                node = node.next;
            }
        }else{
            Node<E> node = first;
            for (int i = 0;i<size;i++){
                if (element.equals(node.element)){
                    return i;
                }
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = get_node(index);
        E old = node.element;
        node.element = element;
        return old;
    }

    @Override
    public boolean contains(Object element) {
        return false;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
    }

    @Override
    public void add(E element) {
        add(size,element);
    }

    @Override
    public void add(int index, E element) {
        if (index == 0){
            first = new Node<E>(element,first);
        }else {
            Node<E> previous_node = get_node(index-1);
            previous_node.next = new Node<E>(element,previous_node.next);

        }
        size++;
    }

    @Override
    public E remove(int index) {
        CheckRange(index);
        Node<E> node = first;
        if (index == 0){
            first = first.next;
        }else {
            Node<E> prev = get_node(index-1);
            node = prev.next;
            prev.next = node.next;
        }
        size--;
        return node.element;
    }

    @Override
    public void ensureCapacity(int capacity) {

    }

    /* 获取index位置对应的节点 */
    private Node<E> get_node(int index){

        // 先检查下标是否越界
        CheckRange(index);

        // 从first开始，写循环一直next，直到指向目标index，一共要循环index次
        Node<E> node = first;
        for (int i = 0;i<index;i++){
            node = node.next;
        }
        return  node;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        Node<E> node = first;
        for (int i = 0;i<size;i++){
            if (i != size-1){
                stringBuilder.append(node.element).append(", ");
            }else {
                stringBuilder.append(node.element);
            }
            node = node.next;
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
